const Controller = require('egg').Controller;
const queryString = require('query-string');


class OrgController extends Controller {

    async create() {
        const body = this.ctx.request.body;
        delete body._csrf;
        if (body.pid != '')
          body.pid = Number(body.pid);
    
        try {
          this.ctx.validate({
            pid: { type: 'int' },
            name: { type: 'string' },
          }, body);
        } catch (err) {
          var message = '';
          err
            .errors
            .map(function (item) {
              message += item.field + ' ' + item.message + '&#10';
            });
    
          const location = '/page/message?' + queryString.stringify({
            status: "warn",
            title: "班级创建失败",
            message: message,
            backTitle: '重新创建',
            backPage: "/page/orgCreate"
          }
          );
    
          this.ctx.body = { location };
    
          return;
        }
    
        const result = await this.ctx.service.org.create(body);
        const information = result.affectedRows === 1 ?
      {
        status: "success",
        title: "记录成功",
        message: '班级创建成功',
        backTitle: '继续创建',
        backPage: "/page/orgCreate"
      } :
      {
        status: "warn",
        title: '创建失败',
        message: '创建失败',
        backTitle: '重新创建',
        backPage: "/page/orgCreate"
      };
    const location = '/page/message?' + queryString.stringify(information);
    const Status = information.status;

    this.ctx.body = { location, Status}

    console.log(this.ctx.body)
        
    }

   async update() {
    const body = this.ctx.request.body;

    delete body._csrf;
    if (body.id != '')
      body.id = Number(body.id);
    if (body.pid != '')
      body.pid = Number(body.pid);

    try {
      this.ctx.validate({
        pid: { type: 'int' },
        id: { type: 'int' },
        name: { type: 'string' },
      }, body);
    } catch (err) {
      var message = '';
      err
        .errors
        .map(function (item) {
          message += item.field + ' ' + item.message + '&#10';
        });

      const location = '/page/message?' + queryString.stringify({
        status: "warn",
        title: "班级修改失败",
        message: message,
        backTitle: '返回班级列表',
        backPage: "/page/orgUpdateList"
      }
      );

      this.ctx.body = { location };

      return;
    }

    if (body.id === body.pid) {
      this.ctx.body = { status: 'error', message: '不能选择自己为上级团队' };
      return;
    }

    const result = await this.ctx.service.org.update(body);
    const information = result.affectedRows === 1 ?
      {
        status: "success",
        title: "修改成功",
        message: '班级记录修改成功',
        backTitle: '返回班级列表',
        backPage: "/page/orgUpdateList"
      } :
      {
        status: "warn",
        title: '修改失败',
        message: '班级修改删除失败',
        backTitle: '返回班级列表',
        backPage: "/page/orgUpdateList"
      };
    const location = '/page/message?' + queryString.stringify(information);

    this.ctx.body = { location };
  }
  
}

module.exports = OrgController;